#include <bits/stdc++.h>
using namespace std;

int T,n,a[200005];
pair<int,int> b[200005];
short color[200005];
long long ans;

bool cmp(pair<int,int> x,pair<int,int> y)
{
    if(x.first!=y.first)
    {
        return x.first<y.first;
    }
    else
    {
        return x.second<y.second;
    }
}

int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--)
    {
        ans=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[i]=make_pair(a[i],i);
        }
        sort(b+1,b+n+1,cmp);
        //bool flag=false;
        int last=-1;
        for(int i=n;i>=1;i--)
        {
            //if(b[i].first==last && !flag) flag=true;
            
            if(b[i].first==last)
            {
                if(color[b[i].second]==0 && color[b[i+1].second]==0)
                {
                    color[b[i].second]=1;
                    color[b[i+1].second]=1;
                    for(int j=b[i].second+1;j<b[i+1].second;j++)
                    {
                        color[j]=2;
                    }
                }
                else if(color[b[i].second]!=0 && color[b[i+1].second]==0)
                {
                    color[b[i+1].second]=color[b[i].second];
                    for(int j=b[i].second+1;j<b[i+1].second;j++)
                    {
                        if(color[j]==0)
                        {
                            color[j]=(3-color[b[i+1].second]);
                        }
                    }
                }
                else if(color[b[i].second]==0 && color[b[i+1].second]!=0)
                {
                    color[b[i].second]=color[b[i+1].second];
                    for(int j=b[i].second+1;j<b[i+1].second;j++)
                    {
                        if(color[j]==0)
                        {
                            color[j]=(3-color[b[i+1].second]);
                        }
                    }
                }
                else if(color[b[i].second]!=0 && color[b[i+1].second]!=0)
                {
                    continue;
                }
            }
            if(b[i].first!=last) last=b[i].first;
        }
        for(int i=1;i<=n;i++)
        {
            if(color[i]==0) color[i]=2;
            int j=i;
            //cout<<color[i]<<" ";
            while(j--)
            {
                if(color[j]==color[i])
                {
                    if(a[i]==a[j]) ans+=a[i];
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}